<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
            <a-form-item label="机构">
              <ak-tree-select-multiple :url="sysDepartUrl.tree" v-model="queryParam.depart.value"/>
            </a-form-item>
            <a-form-item label="姓名">
              <a-input autocomplete="off" placeholder="请输入姓名查询" v-model="queryParam.realname.value"></a-input>
            </a-form-item>
              <a-form-item label="账号">
                <a-input autocomplete="off" placeholder="请输入账号查询" v-model="queryParam.username.value"></a-input>
              </a-form-item>
              <a-form-item label="性别">
                <ak-dict-select-tag dictCode="SexEnum" v-model="queryParam.sex.value"></ak-dict-select-tag>
              </a-form-item>
              <a-form-item label="邮箱">
                <a-input autocomplete="off" placeholder="请输入邮箱查询" v-model="queryParam.email.value"></a-input>
              </a-form-item>

              <a-form-item label="手机号码">
                <a-input autocomplete="off" placeholder="请输入手机号码查询" v-model="queryParam.phone.value"></a-input>
              </a-form-item>

              <a-form-item label="状态">
                <ak-dict-select-tag dictCode="UserStatusEnum" v-model="queryParam.status.value"></ak-dict-select-tag>
              </a-form-item>
          <a-form-item>
            <span  class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a-upload  v-has="'SYS:USER:ADD'"
                         :showUploadList="false"
                         :customRequest="customImportRequest"
                         accept=".xls,.xlsx">
                <a-button  type="primary" :loading="importLoading" icon="import">导入</a-button>
              </a-upload>

              <a-button
                type="primary"
                icon="export"
                @click="handleExport">
                <span v-if="!exporting">导出</span>
                <a-progress v-if="exporting" :percent="exportPercent" size="small" status="active"/>
              </a-button>
              <a-button v-has="'SYS:USER:ADD'" @click="handleAdd"  type="primary" icon="plus">添加用户</a-button>
              <a-dropdown v-if="selectedRowKeys.length > 0">
                <a-menu slot="overlay" @click="handleMenuClick">
                  <a-menu-item key="1" v-has="'SYS:USER:DEL'">
                    <a-icon type="delete" @click="batchDel"/>
                    删除
                  </a-menu-item>
                  <a-menu-item key="2" v-has="'SYS:USER:EDIT'">
                    <a-icon type="lock" @click="batchFrozen('2')"/>
                    冻结
                  </a-menu-item>
                  <a-menu-item key="3" v-has="'SYS:USER:EDIT'">
                    <a-icon type="unlock" @click="batchFrozen('1')"/>
                    解冻
                  </a-menu-item>
                </a-menu>
                <a-button style="margin-left: 8px">
                  批量操作
                  <a-icon type="down"/>
                </a-button>
              </a-dropdown>

            </span>
          </a-form-item>

      </a-form>
    </div>


    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        bordered
        size="middle"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="file" slot-scope="text, record">
          <ak-file-preview :value="text"></ak-file-preview>
        </template>

        <span slot="action" class="table-operator" slot-scope="text, record">
          <a v-has="'SYS:USER:EDIT'" @click="handleEdit(record)">编辑</a>
          <a-popconfirm  v-has="'SYS:USER:EDIT'" v-if="record.status=='NORMAL'" title="确定冻结吗?" @confirm="() => handleFrozen(record.id,'FROZEN')">
                  <a>冻结</a>
            </a-popconfirm>
          <a-popconfirm   v-has="'SYS:USER:EDIT'" v-if="record.status=='FROZEN'" title="确定解冻吗?" @confirm="() => handleFrozen(record.id,'NORMAL')">
                  <a>解冻</a>
                </a-popconfirm>
          <a  v-has="'SYS:USER:EDIT'"  @click="handleChangePassword(record.username)">修改密码</a>
           <a  v-has="'SYS:USER:DEL'"  @click="handleDelete(record.id)">
             删除
           </a>
        </span>


      </a-table>
    </div>
    <!-- table区域-end -->

    <user-modal ref="modalForm" @ok="modalFormOk"></user-modal>

    <password-modal ref="passwordmodal" @ok="passwordModalOk"></password-modal>

  </a-card>
</template>

<script>
  import UserModal from './modules/UserModal'
  import PasswordModal from './modules/PasswordModal'
  import {putAction,getAction} from '../../api/manage';
  import AkfhListMixin from '../../mixin/AkfhListMixin';
  import AkfhModelMixin from '../../mixin/AkfhModelMixin';
  import {sysUserUrl,sysDepartUrl} from '../../api/url'
  import AkDictSelectTag from "../../components/akfh/AkDictSelectTag";
  import AkTreeSelectMultiple from "../../components/akfh/AkTreeSelectMultiple";
  import AkFilePreview from "../../components/akfh/file-preview/AkFilePreview";

  export default {
    name: "UserList",
    mixins: [AkfhListMixin,AkfhModelMixin],
    components: {
      AkFilePreview,
      AkTreeSelectMultiple,
      // AkEditor,
      AkDictSelectTag,
      UserModal,
      PasswordModal
    },
    data() {
      return {
        editIds:'',
        editDepartModal:false,
        description: '这是用户管理页面',
        queryParam: {
          depart:{
            column:'sysDepart.id',
            type:'in',
            value:''
          },

          realname:{
            type:'like',
            value:'',
            column:'realname'
          },
          username:{
            type:'like',
            value:'',
            column:'username'
          },
          sex:{
            type:'eq',
            value:'',
            column:'sex'
          },
          email:{
            type:'like',
            value:'',
            column:'email'
          },
          phone:{
            type:'like',
            value:'',
            column:'phone'
          },
          status:{
            type:'eq',
            value:'',
            column:'status'
          },
        },
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title: '机构',
            align: "center",
            dataIndex: 'sysDepartName'
          },
         

          {
            title: '姓名',
            align: "center",
            dataIndex: 'realname',
          },
          {
            title: '账号',
            align: "center",
            dataIndex: 'username'
          },

          {
            title: '头像',
            align: "center",
            dataIndex: 'headimg_fileUrl',
            scopedSlots: {customRender: "file"}
          },

          {
            title: '性别',
            align: "center",
            dataIndex: 'sex_dictText',
            sorter: true
          },
          {
            title: '生日',
            align: "center",
            dataIndex: 'birthday'
          },
          {
            title: '手机号码',
            align: "center",
            dataIndex: 'phone'
          },
          {
            title: '邮箱',
            align: "center",
            dataIndex: 'email'
          },
          {
            title: '状态',
            align: "center",
            dataIndex: 'status_dictText'
          },
          {
            title: '角色编号',
            align: "center",
            dataIndex: 'roleListCodes'
          },
          {
            title: '角色名称',
            align: "center",
            dataIndex: 'roleListNames'
          },
           {
            title: '负责机构编号',
            align: "center",
            dataIndex: 'leaderDepartsNos'
          },
           {
            title: '负责机构名称',
            align: "center",
            dataIndex: 'leaderDepartsNames'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: {customRender: 'action'},
            align: "center",
            width: 250
          }

        ],
        url: sysUserUrl,
        sysDepartUrl:sysDepartUrl,
      }
    },
    mounted(){
    },
    methods: {


      batchFrozen: function (status) {
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return false;
        } else {
          let ids = "";
          let that = this;
          that.selectedRowKeys.forEach(function (val) {
            ids += val + ",";
          });
          that.$confirm({
            title: "确认操作",
            content: "是否" + (status == 'NORMAL' ? "解冻" : "冻结") + "选中账号?",
            onOk: function () {
              putAction(sysUserUrl.frozenBatch, {ids: ids, status: status}).then((res) => {
                if (res.success) {
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                } else {
                  that.$message.warning(res.message);
                }
              });
            }
          });
        }
      },
      handleMenuClick(e) {
        if (e.key == 1) {
          this.batchDel();
        } else if (e.key == 2) {
          this.batchFrozen('FROZEN');
        } else if (e.key == 3) {
          this.batchFrozen('NORMAL');
        }
      },

      handleFrozen: function (id, status) {
        let that = this;
        putAction(sysUserUrl.frozenBatch, {ids: id, status: status}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          }
        });
      },
      handleChangePassword(username) {
        this.$refs.passwordmodal.show(username);
      },
      passwordModalOk() {
        //TODO 密码修改完成 不需要刷新页面，可以把datasource中的数据更新一下
      }
    }

  }
</script>
<style scoped>
  @import "../../assets/less/common.less";
</style>
